package cn.tradewin.advisor.pages.account;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;
import org.slf4j.Logger;

import cn.tradewin.advisor.exceptions.DBException;
import cn.tradewin.advisor.persist.model.Account;
import cn.tradewin.advisor.persist.model.AccountPerm;
import cn.tradewin.advisor.persist.model.Permissions;
import cn.tradewin.advisor.persist.services.AccountService;
import cn.tradewin.advisor.persist.services.AdvisorSecurityService;

public class AuthorizeAccount {

	@Inject
	private Messages messages;
	
	@Inject
	private BeanModelSource beanModelSource;
	
	@Property
	private BeanModel<Permissions> beanModel;
	
	@Property
	private BeanModel<AccountPerm> beanModelAuthoried;
	
	@Property
	private List<AccountPerm> authorizedPerms;
	
	@Property
	private List<Permissions> availablePerms;
	
	@Property
	private AccountPerm authorized;
	
	@Property
	private Permissions available;
	
	@Inject
	private AccountService accountService;
	
	@Inject
	private AdvisorSecurityService securityService;
	
	private String id;
	
	private String accountName;

	@Inject
	private Logger log;

	public String getCaption() {
		return messages.format("caption",accountName);
	}
	
	void onActivate(String id) throws DBException {
		this.id = id;
	}

	String onPassivate() {
		return id;
	}
	
	@SetupRender
	void setupRender() throws DBException {
		beanModel = beanModelSource.createDisplayModel(Permissions.class, messages);
		beanModel.add("action", null);
		beanModel.include("permId", "permName", "permDesc", "action");
		beanModel.get("action").sortable(false);
		
		beanModelAuthoried = beanModelSource.createDisplayModel(AccountPerm.class, messages);
		beanModelAuthoried.add("action", null);
		beanModelAuthoried.include("permId", "permName", "permDesc", "action");
		beanModelAuthoried.get("action").sortable(false);
		
		Account account = accountService.findAccoutByID(id);
		if (account != null) {
			accountName = account.getLastName()+account.getFirstName();
		}
	}
	
	@OnEvent(EventConstants.PREPARE_FOR_RENDER)
	void prepareForRender() {
		try {
			authorizedPerms = securityService.getAuthorizedPermsForAccount(id);
			availablePerms = securityService.getAvailablePermsForAccount(id);
		} catch (DBException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	
	void onAdd(String permId) {
		try {
			securityService.addPermToAccount(id, permId);
		} catch (DBException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e.getMessage());
		}
	}
	
	void onDelete(String permId) {
		try {
			securityService.removePermFromAccount(id, permId);
		} catch (DBException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
}
